package com.rongshu.view.penpal.present;

import android.content.Context;
import android.content.Intent;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.penpal.bean.PenpalArticleResponse;
import com.rongshu.view.penpal.bean.PenpalRecommendArticleResponse;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/22.
 * description：
 */
public class PenpalSearchArticleListPresenter extends BasePresenter<INetView> {

    public PenpalSearchArticleListPresenter(Context context) {
        super(context);
    }

    public void penpalSearchArticleList(int penpalwill_id, String searchWhere, int type, int pageIndex, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        map.put("searchWhere", searchWhere);
        map.put("type", type);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        RetorfitService.penpalSearchArticleList(UserInfoUtils.getUserId(), penpalwill_id, searchWhere, type, pageIndex, pageSize,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<PenpalArticleResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PenpalArticleResponse object) {
                getView().success(object, 0);
            }
        });
    }

    public void toArticleDetailsAct(PenpalRecommendArticleResponse.ResponseBean response) {
        Intent intent = new Intent(mContext, SingleArticleDetailsActivity.class);
        intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, convertBean(response));
        mContext.startActivity(intent);
    }

    private ArticleDetaisRequest convertBean(PenpalRecommendArticleResponse.ResponseBean response) {
        ArticleDetaisRequest bean = new ArticleDetaisRequest();
        bean.setContribute_id(response.getContribute_id());
        bean.setType(Constant.FROM_PENPAL);
        bean.setTopic_id(response.getTopic_id());
        return bean;
    }
}
